package org.example.service;

import jakarta.annotation.Resource;
import org.example.dao.IUserRepository;
import org.example.entity.User;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Resource
    private IUserRepository userRepository;

    public User findByUserIdAndPassword(String id, String pw){
        return userRepository.findByUserIdAndPassword(id,pw);
    }
    public int countUserById(String id){
        return userRepository.countByUserId(id);
    }

    public User addUser(String userId, String password, String userName, Integer userSex){
        if(userRepository.countByUserId(userId)==0){
            User user = new User();
            //这里虽然只接受4个参数, 但是如果要传递给userService的话, 需要所有东西都来set一遍之后, 再传user实例
            user.setUserId(userId);
            user.setPassword(password);
            user.setUserSex(userSex);
            user.setUserName(userName);
            user.setDelTag(1);//tag=1,表明用户未被删除, 新增用户默认为1
            user.setUserImg("");//设为空字符串,给一个默认值,而非给一个null
            return userRepository.save(user);
        }
        return null;//如果已有用户名,那么直接返回空
    }

    public User findUserById(String userId) {
        return userRepository.findByUserId(userId);
    }
}
